package com.tuoyu.peopletest.view.custom;

import android.content.Context;
import android.util.AttributeSet;

/**
 * 宽高相等的imageview
 */
public class WHEImageView extends androidx.appcompat.widget.AppCompatImageView {


    public WHEImageView(Context context) {
        super(context);
    }

    public WHEImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WHEImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //通过计算,做到宽高相等
        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
        } else if (heightSpecMode == MeasureSpec.EXACTLY) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
